package com.foreveredu.erlms.web.struts.action;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.foreveredu.erlms.domain.Enroll;
import com.foreveredu.erlms.domain.Examinee;
import com.foreveredu.erlms.domain.PageModel;
import com.foreveredu.erlms.service.IExamService;
import com.foreveredu.erlms.service.IManagementService;
import com.foreveredu.erlms.web.struts.actionform.EnrollActionForm;

public class EnrollAdminAction extends AbstractBaseDispatchAction {


	private IExamService examService = null;
	private IManagementService managementService = null;

	public IExamService getExamService() {
		return examService;
	}

	public void setExamService(IExamService examService) {
		this.examService = examService;
	}

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}
	Logger log = Logger.getLogger(EnrollAction.class);

	//后台考生信息翻页列表
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 获取当前考次
		int examOrder = examService.getExamOrderOfNow();

		int offset = 0;
		String pageOffset = request.getParameter("pager.offset");// 获得翻页参数，即下一开始的纪录位置
		if (pageOffset != null && !pageOffset.equals("")) {
			offset = Integer.parseInt(pageOffset);
		}
		if (offset < 0) {
			return mapping.findForward("enrollList");
		}
		PageModel pm = examService.findEnrollList(offset, examOrder);
		request.setAttribute("pm", pm);
		request.setAttribute("offset", offset);
		
		return mapping.findForward("enrollList");
	}
	
	//查询考生已交费未交费报名人数等信息
	public ActionForward statisticInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 获取当前考次
		int examOrder = examService.getExamOrderOfNow();

		//报名人数数据  
		int EnrollAllNumber =examService.findEnrollAllNumber(examOrder); 
		int EnrollPaidNumber =examService.findEnrollPaidNumber(examOrder);
		int EnrollNotPaidNumber = EnrollAllNumber - EnrollPaidNumber;
		
		//第37次考试取消补考
//		int EnrollKeepGradeNumber = examService.findEnrollKeepGradeNumber(examOrder);
//		int EnrollPaidNumberByTodayAndKeepGrade =examService.findEnrollPaidNumberByTodayAndKeepGrade(now);
//		int EnrollPaidNumberByTodayAndEnroll =EnrollPaidNumberByToday - EnrollPaidNumberByTodayAndKeepGrade ;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String now = sdf.format(new Date());
		int EnrollPaidNumberByToday = examService.findEnrollPaidNumberByToday(now);
		
		request.setAttribute("EnrollAllNumber", EnrollAllNumber);
		request.setAttribute("EnrollPaidNumber", EnrollPaidNumber);
		request.setAttribute("EnrollNotPaidNumber", EnrollNotPaidNumber);
		request.setAttribute("EnrollPaidNumberByToday", EnrollPaidNumberByToday);
//		request.setAttribute("EnrollKeepGradeNumber", EnrollKeepGradeNumber);
//		request.setAttribute("EnrollPaidNumberByTodayAndKeepGrade", EnrollPaidNumberByTodayAndKeepGrade);
//		request.setAttribute("EnrollPaidNumberByTodayAndEnroll", EnrollPaidNumberByTodayAndEnroll);
		
		return mapping.findForward("statisticInfo");
	}
	
	//后台条件查询学生信息
	public ActionForward selectEnroll(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 获取当前考次
		int examOrder = examService.getExamOrderOfNow();

		EnrollActionForm enrollForm = (EnrollActionForm) form;
		PageModel pm = new PageModel();
		pm.setTotal(-1);

		if (enrollForm.getIdCard() != null
				&& "".equals(enrollForm.getSerialNumber())) {
			List<Enroll> enroll = examService.findEnrollByIdCard(
					enrollForm.getIdCard(), examOrder);
			if (enroll != null && !"".equals(enroll)) {
				pm.setDatas(enroll);
				
				request.setAttribute("pm", pm);
				return mapping.findForward("selectEnroll");
			}
		}
		// 通过网报号查找考生
		if ("".equals(enrollForm.getIdCard())
				&& enrollForm.getSerialNumber() != null) {
			Examinee examinee = examService.findExamineeBySerialNumber(
					enrollForm.getSerialNumber(),
					examService.getExamOrderOfNow());
			if (examinee != null) {
				List<Enroll> enroll = examService
						.findEnrollByExamineeId(examinee.getId());
				pm.setDatas(enroll);
				
				request.setAttribute("pm", pm);
				return mapping.findForward("selectEnroll");
			}
		}

		if (enrollForm.getIdCard() != null
				&& enrollForm.getSerialNumber() != null) {
			List<Enroll> enrollList = new ArrayList<Enroll>();
			Enroll enroll = examService.findEnrollByIdCardAndSerialNumber(
					enrollForm.getIdCard(), enrollForm.getSerialNumber(),
					examOrder);
			enrollList.add(enroll);
			if (enrollList != null) {
				pm.setDatas(enrollList);
				
				request.setAttribute("pm", pm);
				return mapping.findForward("selectEnroll");
			}
		}
		request.setAttribute("pm", pm);
		return mapping.findForward("selectEnroll");
	}
	// 删除考生信息
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		EnrollActionForm enrollForm = (EnrollActionForm) form;
		examService.deleteEnrollById(enrollForm.getId());
		return mapping.findForward("toEnrollList");
	}
	
	// 改变报名信息交费状态
	public ActionForward updateEnrollPaid(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
	
		EnrollActionForm enrollForm = (EnrollActionForm) form;
		Enroll enroll = examService.findEnrollById(enrollForm.getId());
	
		if (enroll.getIsPaid() == 0) {
			// 当考生交费后，生成该考生的报名号，并修改该考生的相片的文件名为报名号
			String enrollNumber = examService.getMaxENRecord(enroll.getRank().getCode());
			Examinee examinee = enroll.getExaminee();
			examinee.setEnrollNumber(enrollNumber);
			examService.updateExaminee(examinee);
	
			String photo = request
					.getSession()
					.getServletContext()
					.getRealPath(
							"/photo/" + enroll.getIcon());
			File file = new File(photo);
			log.info(photo);
			
			String newPhoto = request.getSession().getServletContext()
					.getRealPath("/photo/" + enrollNumber + ".jpg");
			File newFile = new File(newPhoto);
			log.info(newPhoto);
			
			if (file != null) {
				file.renameTo(newFile);
			}
			
			enroll.setIcon(enrollNumber + ".jpg");
			
			//将当前日期储存到enroll中
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String now = sdf.format(new Date());
			enroll.setPaidTime(now);
			enroll.setIsPaid(1);
			
			examService.updateEnroll(enroll);
			
			
			// 获取当前考次
//			int examOrder = examService.getExamOrderOfNow();

			int offset = 0;
			String pageOffset = request.getParameter("offset");// 获得翻页参数，即下一开始的纪录位置
			if (pageOffset != null && !pageOffset.equals("")) {
				offset = Integer.parseInt(pageOffset);
			}
			if (offset < 0) {
				return mapping.findForward("paidToEnrollList");
			}
//			PageModel pm = examService.findEnrollList(offset, examOrder);
//			request.setAttribute("pm", pm);
			request.setAttribute("offset", offset);
			
			return mapping.findForward("paidToEnrollList");
		}
	
		return mapping.findForward("paidToEnrollList");
	}
	
	
	
	// 通过ID查询学生详细信息（enroll）
	public ActionForward findEnrollById(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		EnrollActionForm enrollForm = (EnrollActionForm) form;
		Enroll enroll = examService.findEnrollById(enrollForm.getId());
		request.setAttribute("enroll", enroll);
		return mapping.findForward("findEnrollById");
	}
		
}
